package app.server.project.main.service.controller.cms;

import app.server.project.core.exception.SysError;
import app.server.project.core.http.CMSLstResp;
import app.server.project.core.http.RespModel;
import app.server.project.core.httpauth.UnAuth;
import app.server.project.core.token.TokenCache;
import app.server.project.core.util.AssertUtil;
import app.server.project.main.service.dto.AddEnergyReq;
import app.server.project.main.service.dto.AddLvReq;
import app.server.project.main.service.dto.cms.UploadFileDto;
import app.server.project.main.service.service.cms.GMService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@AllArgsConstructor
@RestController
@RequestMapping("/cmsapi/gm")
@Slf4j
public class CMSGMController {

    private GMService gmService;


    /**
     * 改变用户等级
     *
     * @param req
     */
    @PostMapping("/addLv")
    public RespModel addLv(@RequestBody AddLvReq req) {
        this.gmService.addLv(req);
        return RespModel.success();
    }

    /**
     * 添加体力
     *
     * @param req
     */
    @PostMapping("/addEnergy")
    public RespModel addEnergy(@RequestBody AddEnergyReq req) {
        this.gmService.addEnergy(req);
        return RespModel.success();
    }

    /**
     * 上传客户端代码
     *
     * @param req
     */
    @UnAuth
    @PostMapping("/uploadGame/{userId}/{token}/{version}")
    public RespModel uploadGame(@RequestParam("Fdata") MultipartFile[] files, @PathVariable String version, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.gmService.upload(files, version);
        return RespModel.success();
    }

    /**
     * 拉取客户端版本
     *
     * @return
     */
    @UnAuth
    @RequestMapping("/getVersion")
    public RespModel<String> getVersion() {
        return RespModel.success(this.gmService.getVersion());
    }

    /**
     * 拉取客户端版本列表
     *
     * @return
     */
    @PostMapping("/getUploadFiles")
    public CMSLstResp<UploadFileDto> getUploadFiles() {
        return this.gmService.getUploadFiles();
    }
}
